package com.lijiayue.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lijiayue.LoginUtil.LjYLogin;
import com.lijiayue.model.domain.MentalData;
import com.lijiayue.model.domain.Student;
import com.lijiayue.model.Upload;
import com.lijiayue.model.dto.MentalDataDTO;
import com.lijiayue.service.MentalDataService;
import com.lijiayue.service.StudentService;
import com.lijiayue.util.AuthContextHolder;
import com.lijiayue.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Slf4j
@Tag(name = "学生接口")
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @Resource
    private MentalDataService mentalDataService;

    @LjYLogin
    @Operation(summary = "查询当前学生信息")
    @GetMapping("/info")
    public Result<Student> getStudentById() {
        Long userId = AuthContextHolder.getUserId();
        Student student = studentService.getById(userId);
        return Result.ok(student);
    }

    @LjYLogin
    @PostMapping("/avatar")
    @Operation(summary = "上传用户头像")
    public Upload uploadAvatar(@RequestPart("file") MultipartFile file) {
        if (file.isEmpty()) {
            throw new RuntimeException("上传文件不能为空");
        }
        //获得文件扩展名
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = null;
        try {
            url = studentService.upload(extension, new ByteArrayInputStream(file.getBytes()));
        } catch (IOException e) {
            log.error("文件上传失败,原因：", e);
        }
        Upload upload = new Upload();
        upload.setUrl(url);
        return upload;
    }

    @LjYLogin
    @PostMapping("/info")
    @Operation(summary = "更新当前学生信息")
    public Result<String> updateStudentInfo(@RequestBody Student student) {
        Long userId = AuthContextHolder.getUserId();
        student.setId(userId);
        boolean update = studentService.updateById(student);
        if (update) {
            return Result.ok("更新成功");
        }
        return Result.fail("更新失败");
    }


    @LjYLogin
    @PostMapping("/mental/collect")
    @Operation(summary = "收集心理数据")
    public Result<String> collectMentalData(@RequestBody MentalDataDTO mentalDataDTO) {
        MentalData mentalData = BeanUtil.copyProperties(mentalDataDTO, MentalData.class);

        Long userId = AuthContextHolder.getUserId();
        mentalData.setUserId(userId);
        mentalData.setId(IdUtil.getSnowflakeNextId());
        mentalData.setCreateTime(new Date());
        boolean save = mentalDataService.save(mentalData);
        if (save) {
            return Result.ok("收藏成功");
        }
        return Result.fail("收藏失败");
    }

    @LjYLogin
    @GetMapping("/mental/history")
    @Operation(summary = "查询心理数据历史")
    public Result<MentalData> getMentalDataHistory() {

        MentalData mentalData = mentalDataService.getMentalDataHistory();
        return Result.ok(mentalData);
    }

    @LjYLogin
    @GetMapping("/mental/list")
    @Operation(summary = "查询心理数据列表")
    public Result<List<MentalData>> getList() {

        List<MentalData> mentalDataList = mentalDataService.getList();
        return Result.ok(mentalDataList);
    }


}
